<template>
	<view class="flex-column" style="height: 100%;">
		<view class="" style="flex-grow: 1;">
			<view v-for="(item,index) in dataList" :key="index">
				<view class="row-lineConter">
					<ResourceCell :dataModel="item" ></ResourceCell>
					<view @click="delFileHandle(index)" class="zsConter" style="border-radius: 4px; padding: 0px 6px; background-color: red; color: white;">
						删除
					</view>
				</view>
			</view>
		</view>
		
		<view class="" style="padding: 20px 40px; ">
			<view class="row-lineConter ">
				<view :class="item == page? 'SpageItem':'pageItem'"  @click="pageClick(item)" v-for="(item,index) in totalPageCount" :key="index">
					{{item}}
				</view>
			</view>
		</view>
		
	</view>
</template>

<script>
	import ResourceCell from './resource-cell.vue'
	
	export default {
		components: {
			ResourceCell
		},
		data() {
			return {
				totalCount:0,
				dataList:[],
				totalPageCount:0,
				type:'推荐',
				page:1,
			}
		},
		onLoad() {
		  this.getTotalCount();
		  
		},
		methods: {
			getTotalCount(){
				this.$uniCloudApi.get_resource_count().then(res=>{
					this.totalCount = res;
				     var integer= Math.floor(this.totalCount / 10);
					 let remainder = this.totalCount % 10; // 计算余数
					 if(remainder > 0){
						 this.totalPageCount = integer + 1;
					 } else {
						  this.totalPageCount = integer;
					 }
					this.getDataList();
				});
			},
			getDataList(){
				this.$uniCloudApi.get_data_list(this.type,this.page,10).then(res=>{
					this.dataList = res;
				});
			},
			pageClick(page){
				this.page = page;
				this.$uniCloudApi.get_data_list(this.type,this.page,10).then(res=>{
					this.dataList = res;
				});
				
				let remainder = 60 % 10; // 计算余数
				console.log('余数==',remainder); // 输出余数
			},
			// 删除文件
			async delFileHandle(index){
				uni.showLoading({
					title:'删除中...'
				})
				var item = this.dataList[index]
				var urls = item.resourceImages
				console.log('图片数组=',urls);
			    await uniCloud.callFunction({
			        name: 'delete-image-file', // 云函数名称
			        data: {
			            fileList: urls// 删除的文件 ID 数组
			        },
			        success: (res) => {
			            console.log('删除结果:', res.result);
						this.$uniCloudApi.delete_resources_id(item._id).then(dataRes=>{
							uni.hideLoading()
							this.dataList.splice(index,1);
							this.$showToast('删除成功')
							this.$forceUpdate();
						}).catch(dataRes=>{
							uni.hideLoading()
							this.$showToast(dataRes);
						});
			        },
			        fail: (error) => {
			            console.error('删除失败:', error);
			        }
			    })
			}
		}
	}
</script>

<style lang="scss" scoped > 
 .pageItem{
	  padding: 5px 10px;
	  border: 1px solid #c0c0c0; /* 设置边框颜色为金色 */
	  font-size: 12px;
	  color: black;
	  margin-right: 4px;
	  border-radius: 4px;
  }
  .SpageItem{
  	  padding: 5px 10px;
  	  border: 1px solid #c0c0c0; /* 设置边框颜色为金色 */
  	  font-size: 12px;
  	  color: black;
  	  margin-right: 4px;
  	  border-radius: 4px;
	  background-color: rgba(52, 116, 246,0.5);
  }
</style>
